from Base.ReadExcel import Read_Excel
from Base.HttpBase import HttpBase
from Base.WriteExcel import Write_excel
from RequestService.LoginGetCookie import getCookie
import os, time, json
from Base import Log, GetYamlFile

log = Log.Log()
cookie = getCookie()

def getAllCase():
    # openpyxl模块读写从数字1开始 所以第二行去数字2
    num = 2
    test_case = []
    YmlList = GetYamlFile.getYamlFile()
    TestCasePath = YmlList["TestCasePath"]
    TestDomainName = YmlList["TestDomainName"]
    PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, TestCasePath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    #获取域名
    url = readBook.read_Excel(TestDomainName, 1, 0)
    for sheetNames in readBook.get_SheetName():
        # 过滤域名TestDomainName
        get_head_name_num = get_excel_head_name_num(sheetNames)
        if sheetNames != TestDomainName:
            allNum = readBook.get_RowNums(sheetNames)
            for itemNum in range(1, allNum):
                # print(readBook.read_Excel("Request",itemNum,0))
                # 获取测试名称
                testName = readBook.read_Excel(sheetNames, itemNum, get_head_name_num["testName"])
                # 获取请求方式
                method = readBook.read_Excel(sheetNames, itemNum, get_head_name_num["method"])
                # 获取url
                url2 = readBook.read_Excel(sheetNames, itemNum, get_head_name_num["URL"])
                # 获取参数
                params = readBook.read_Excel(sheetNames, itemNum, get_head_name_num["params"])
                # 获取预期结果
                hopeResult = readBook.read_Excel(sheetNames, itemNum, get_head_name_num["hopeResult"])
                # 拼接完整的url
                urlAll = url + url2
                # 拼接result的case名称
                testName_all = sheetNames + "-" + testName
                if urlAll[-1] =="}":
                    if str(params).split("."):
                        params=str(params).split(".")[0]
                    else:
                        pass
                    urlAll=urlAll.split("{")[0]+str(params)
                    excelData = {
                        "rowNum": num,
                        "testName": testName_all,
                        "method": method,
                        "url": urlAll,
                        "params": "",
                        "hopeResult": hopeResult
                    }
                else:
                    # 用字典形式读取单个case
                    excelData = {
                        "rowNum": num,
                        "testName": testName_all,
                        "method": method,
                        "url": urlAll,
                        "params": params,
                        "hopeResult": hopeResult
                    }
                num += 1
                # 添加用例名称后续报告注释输出
                items = (testName, excelData)
                # 用数组形式写入所有case
                test_case.append(items)
    log.info("获取所有的用例结果集：" + str(test_case))
    return test_case

'''
获取首行名称
'''
def get_excel_head_name_num(sheetNames):
    get_testname_num = 0
    get_method_num = 0
    get_URL_num = 0
    get_params_num = 0
    get_hopeResult_num = 0
    YmlList = GetYamlFile.getYamlFile()
    TestCasePath = YmlList["TestCasePath"]
    PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, TestCasePath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    for item in range(0, readBook.get_CellNums(sheetNames)):
        head_name = readBook.read_Excel(sheetNames, 0, item)
        # print(head_name)
        if head_name == "testname":
            get_testname_num = item
        elif head_name == "method":
            get_method_num = item
        elif head_name == "URL":
            get_URL_num = item
        elif head_name == "params":
            get_params_num = item
        elif head_name == "hopeResult":
            get_hopeResult_num = item
    get_head_name_num = {
        "testName": get_testname_num,
        "method": get_method_num,
        "URL": get_URL_num,
        "params": get_params_num,
        "hopeResult": get_hopeResult_num
    }
    return get_head_name_num